var util = require('../../util/util.js')
var formatLocation = util.formatLocation

var app = getApp();
var bmap = require('../../libs/bmap-wx.js');
var wxMarkerData = []; 
var address;
var id_now;
let c_location;
Page({
  data: {
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [{ //假数据
      img: "avatar.png",
      name: "欢顔",
      tag: "知名情感博主",
      answer: 134,
      listen: 2234,
      inputTxt: ''
    }],
    // address:[{
    //   name:'徐州爱琴海商务酒店',
    //   detail:'绿地商务城LOft3楼'
    // }],
   
    history_flag:true,
    
    
    // 百度地图
    // markers: [],
    // latitude: '',
    // longitude: '',
    // placeData: {} 

  },
  // makertap: function (e) {
  //   var that = this;
  //   var id = e.markerId;
  //   that.showSearchInfo(wxMarkerData, id);
  //   that.changeMarkerColor(wxMarkerData, id);
  // }, 
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function (options) {
     var that = this;
     that.setData({
       historyList: [
         {
           name: "万达广场",
           address: "徐州市云龙区",
           latitude:"34.2885",
           longitude:"117.18554"
         }, {
           name: "徐州万创网络科技有限公司",
           address: "徐州市鼓楼区黄河北路90号",
            latitude: "34.2885",
           longitude: "117.18554"
         }, {
           name: "徐州百度公司",
           address: "徐州市鼓楼区黄河北路91号",
           latitude: "34.2885",
           longitude: "117.18554"
         }, {
           name: "徐州美团",
           address: "徐州市鼓楼区黄河北路92号",
           latitude: "34.2885",
           longitude: "117.18554"
         }, {
           name: "饭局宾馆",
           address: "徐州市鼓楼区黄河北路93号",
           latitude: "34.2885",
           longitude: "117.18554"
         }
       ]
     })
    
    //  id_now=options.id;

    //  20180712modify
     ///获取定位信息
     wx.getLocation({
       success: function (res) {
          that.setData({
            location: formatLocation(res.longitude, res.latitude),
            latitude:res.latitude,
            longitude:res.longitude,
            
            markers: [{
              id: 1,
              latitude: res.latitude,
              longitude: res.longitude,

            }],
            covers: [{
              latitude: res.latitude,
              longitude: res.longitude,
              iconPath: 'images/location.png'
            }, {
                latitude: res.latitude,
                longitude: res.longitude,
                iconPath: 'images/location.png'
            }]
         })


       }
     })

     


    //  console.log(id_now);
    //  that.setData({
    //    id_now:options.id
       
    //  })

    

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        // console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });

  // 百度地图
  
    // 新建百度地图对象 
    // var BMap = new bmap.BMapWX({
    //   ak: 'bzQsfmU86UsaqMOeZzXFrjbkVAUKcVYW'
    // });
    // var fail = function (data) {
    //   console.log("error"+data)
    // };
    // var success = function (data) {
    //   console.log("success**********");
    //   console.log(data);
    //   // console.log(data.wxMarkerData[0]['title'])
    //   // 百度地图放置位置
    //   that.setData({
    //     address: data.wxMarkerData
    //   });

      // wxMarkerData = data.wxMarkerData;
      // that.setData({
      //   markers: wxMarkerData
      // });
      // that.setData({
      //   latitude: wxMarkerData[0].latitude
      // });
      // that.setData({
      //   longitude: wxMarkerData[0].longitude
      // });

    }
    // 发起POI检索请求 
    // BMap.search({
    //   "query": '小区',
    //   fail: fail,
    //   success: success,
    //   // 此处需要在相应路径放置图片文件 
    //   iconPath: '../../img/marker_red.png',
    //   // 此处需要在相应路径放置图片文件 
    //   iconTapPath: '../../img/marker_red.png'
    // });


  // },
 ,chooseLocation: function () {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          c_location: formatLocation(res.longitude, res.latitude),
          locationAddress: res.address,
          c_latitude:res.latitude,
          c_longitude:res.longitude
        })
       
      }
    })
  },


  // showSearchInfo: function (data, i) {
  //   var that = this;
  //   that.setData({
  //     placeData: {
  //       title: '名称：' + data[i].title + '\n',
  //       address: '地址：' + data[i].address + '\n',
  //       // telephone: '电话：' + data[i].telephone
  //     }
  //   });
  // },

  formBindsubmit: function (mobile) {
    console.log("mobile");
    console.log(mobile)
    var transmit = mobile.detail.value;
    console.log(mobile.detail.value);
    var mobile = mobile.detail.value.phone; //传入首页的手机号
    var name = transmit.setname; //传入首页的用户名
    var address_1 = transmit.address1; //传入首页的地址1
    var address_2 = transmit.address2; //传入首页的详细地址
    
    // id_now=transmit.id_now;
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    else{
    }
   
    
    if(!name){
      wx.showToast({
        title: '收货人姓名不能为空',
        icon:"none",
      })
      return false;
    }else if(!address_1){
      wx.showToast({
        title: '收货地址不能为空',
        icon:"none",
      })
      return false;
    }else{
      // wx.showToast({
      //   title: '验证成功',
      //   icon: "success",
      //   duration:1500
      // })
      // wx.reLaunch({
      //     url: '/pages/index/index?mobile=' + mobile + "&name=" + name + "&address_1=" + address_1 + "&address_2=" + address_2                  + "&id_now=" + id_now
      //   })

    }

    // wx.request({
    //   url: '',
    //   data:{
    //     mobile: mobile,
    //     name: name,
    //     address_1: address_1,
    //   },
    //   success:function(){
        wx.reLaunch({
          url: '/pages/index/index?mobile=' + mobile + "&name=" + name + "&address_1=" + address_1 + "&address_2=" + address_2 + "&id_now=" + id_now
        })
      // }
    // })

 return true;
    
  },

// 点击下方地址选择回填到上方
  getadd:function(e){
    console.log(e);
    let address = e.currentTarget.dataset.add;
    
    console.log(address)
    this.setData({
      locationAddress: address,
      c_latitude: e.currentTarget.dataset.c_latitude,
      c_longitude:e.currentTarget.dataset.c_longitude
    })
  },

  changeMarkerColor: function (data, i) {
    var that = this;
    var markers = [];
    for (var j = 0; j < data.length; j++) {
      if (j == i) {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_yellow.png";
      } else {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_red.png";
      }
      markers[j](data[j]);
    }
    that.setData({
      markers: markers
    }); 

  },
  skiped:function(){
    // wx.navigateTo({
    //   url: '/pages/index/index',
    // })
  },
  footerTap: app.footerTap
})